﻿Imports System.IO
Imports System.Xml
Imports System.Text
Public Class BlockOneHomeworkSectionSix
    Dim filename As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
    Dim ArrayMax As Integer = 26 'One number past the end of actual info in array
    Dim intResponse, intResponse2, intResponse3 As Integer
    Dim strUser As String = "Enter Last Name"

    Shared Property MidiParent As MainForm

    Private Sub CloseToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles CloseToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub BlockOneSectionSix_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        txtObj.Text = "Objectives:" + vbCrLf + "- Given proper student instructional materials, describe basic principles of Meteorology."
        If My.Settings.B1S6QC = 1 Then
            strUser = InputBox("Please enter last name", strUser)
            If strUser Is "" Then
                Me.Close()
            End If
        End If

        If My.Settings.B1S6QC > ArrayMax Then My.Settings.B1S6QC = ArrayMax

        If My.Settings.B1S6QC > 1 And My.Settings.B1S6QC < ArrayMax Then
            intResponse2 = MsgBox(" This assignment is already in progress. Do you want to continue?", 4, "Assignment in Progress")
            If intResponse2 = MsgBoxResult.Yes Then
                My.Settings.B1S6QC = My.Settings.B1S6QC - 1
                'Do Nothing
            ElseIf intResponse2 = MsgBoxResult.No Then
                intResponse = MsgBox("Do you want to start this over from the beginning?", 4, "Start back at the beginning?")
                If intResponse = MsgBoxResult.Yes Then
                    My.Settings.B1S6QC = 1
                ElseIf intResponse = MsgBoxResult.No Then
                    Me.Close()
                End If
            End If
        End If

        If My.Settings.B1S6QC = ArrayMax Then
            intResponse3 = MsgBox("This assignment has already been completed.  Are you sure you want to delete the current assignment and reaccomplish it?", 4, "Confirm Assignment Deletion")
            If intResponse3 = MsgBoxResult.Yes Then
                System.IO.File.Delete(filename & "\Homework\Block 1\Block 1 Section 6.txt")
                System.IO.File.Create(filename & "\Homework\Block 1\Block 1 Section 6.txt").Dispose()
                My.Settings.B1S6QC = 1
            ElseIf intResponse3 = MsgBoxResult.No Then
                Me.Close()
            End If
        End If

        If My.Settings.B1S6QC >= ArrayMax Then
            My.Settings.B1S6QC = 1
        End If
        GetXMLData()
        Next_Question()

    End Sub

    Private Sub btnNextQuestion_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNextQuestion.Click
        MainForm.strB1S6A(My.Settings.B1S6QC) = txtAnswer.Text '**** Linus 12 July ****
        My.Settings.B1S6QC = My.Settings.B1S6QC + 1
        txtAnswer.Clear()
        Next_Question()
    End Sub

    Public Sub Next_Question()
        txtAnswer.Focus()

        If btnNextQuestion.Text = "Done" Then
            AddXMLData()
            WriteToFile()
            Me.Close()
            OpenResults()
        End If

        If My.Settings.B1S6QC = ArrayMax Then '**** Linus 12 July ***
            btnNextQuestion.Text = "Done"
        End If

        If My.Settings.B1S6QC <= ArrayMax Then '**** Linus 12 July ****
            txtQuestion.Text = MainForm.strB1S6Q(My.Settings.B1S6QC)
            txtAnswer.Text = MainForm.strB1S6A(My.Settings.B1S6QC)
        End If
    End Sub

    Private Sub WriteToFile()
        Dim Integrity As String = (filename) & "\Instructor\Block 1\Block 1 Section 6.txt"
        Dim Results As String = (filename) & "\Homework\Block 1\Block 1 Section 6.txt"
        Dim k As Integer = 0 '**** Linus 12 July ****

        If System.IO.File.Exists(filename & "\Homework\Block 1\Block 1 Section 6.txt") Then
            System.IO.File.Delete(filename & "\Homework\Block 1\Block 1 Section 6.txt")
            System.IO.File.Create(filename & "\Homework\Block 1\Block 1 Section 6.txt").Dispose()
        Else
            System.IO.File.Create(filename & "\Homework\Block 1\Block 1 Section 6.txt").Dispose()
        End If

        '***********Output to hidden file for Instructor Check***********
        If System.IO.File.Exists(Integrity) = True Then
            Dim hiddenWriter As New System.IO.StreamWriter(Integrity, True)
            hiddenWriter.WriteLine(DateTime.Now)
            hiddenWriter.WriteLine("- " & txtObj.Text)
            hiddenWriter.WriteLine()
            hiddenWriter.WriteLine("Student: " & strUser)
            hiddenWriter.WriteLine()
            For k = 1 To ArrayMax '**** Linus 12 July *****
                hiddenWriter.WriteLine("Question: " & MainForm.strB1S6Q(k))
                hiddenWriter.WriteLine("Answer: " & MainForm.strB1S6A(k))
                'hiddenWriter.WriteLine("Reference: " & MainForm.strB1S1R(k))
                hiddenWriter.WriteLine()
            Next k '***** Linus 12 July *****

            hiddenWriter.WriteLine("Block 1 Section 6 Complete")
            hiddenWriter.Close()

            'hiddenWriter.WriteLine(DateTime.Now) '**** Linus 12 July (was giving an error) ****
        End If

        '******Output to file for Student to review***********
        If System.IO.File.Exists(Results) = True Then
            Dim hiddenWriter As New System.IO.StreamWriter(Results, True)
            hiddenWriter.WriteLine("- " & txtObj.Text)
            hiddenWriter.WriteLine()
            hiddenWriter.WriteLine("Student: " & strUser)
            hiddenWriter.WriteLine()
            For k = 1 To ArrayMax '**** Linus 12 July ****
                hiddenWriter.WriteLine("Question: " & MainForm.strB1S6Q(k))
                hiddenWriter.WriteLine("Answer: " & MainForm.strB1S6A(k))
                hiddenWriter.WriteLine()
                'hiddenWriter.WriteLine("Reference: " & MainForm.strB1S6R(k))
                'hiddenWriter.WriteLine() '**** Linus (I think you ment comment this out) ****
            Next k '**** Linus 12 July ****
            hiddenWriter.WriteLine("Block 1 Section 6 Complete")
            hiddenWriter.Close()
        Else
            MsgBox("File Does Not Exist")
        End If
    End Sub

    Private Sub OpenResults()
        Dim p As Process = Process.Start((filename) & "\Homework\Block 1\Block 1 Section 6.txt")
    End Sub

    Private Sub btnPreviousQuestion_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPreviousQuestion.Click

        If My.Settings.B1S6QC = 0 Then Return '***** Linus 11 July *****

        If My.Settings.B1S6QC = ArrayMax Then '**** Linus 12 July *** Then
            btnNextQuestion.Text = "Next Question"
        End If

        My.Settings.B1S6QC = My.Settings.B1S6QC - 1

        txtQuestion.Text = MainForm.strB1S6Q(My.Settings.B1S6QC)
        txtAnswer.Text = MainForm.strB1S6A(My.Settings.B1S6QC) ' **** Linus 12 July ****
        txtAnswer.Focus()

    End Sub

    Private Sub OnKeyDownHandler(ByVal sender As Object, ByVal e As KeyEventArgs) Handles txtAnswer.KeyDown
        If (e.KeyCode = Keys.Enter) Then
            MainForm.strB1S6A(My.Settings.B1S6QC) = txtAnswer.Text
            My.Settings.B1S6QC = My.Settings.B1S6QC + 1
            Next_Question()
        End If
    End Sub

    Private Sub BlockOneHomeworkSectionOne_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        My.Settings.Save()
    End Sub

    Private Sub txtAnswer_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtAnswer.TextChanged
        txtAnswer.Text = txtAnswer.Text.Trim
    End Sub

    Public Sub GetXMLData()
        Dim xmlReader As New XmlTextReader("C:\Temp\B1S6AFSData.xml")

        While xmlReader.Read()
            For i = 0 To ArrayMax
                If xmlReader.Name.ToString() = "Answer" & i Then
                    MainForm.strB1S6A(i) = xmlReader.ReadString().Replace("None", "")
                End If
                If xmlReader.Name.ToString() = "Question" & i Then
                    MainForm.strB1S6Q(i) = xmlReader.ReadString()
                End If
            Next
        End While
        xmlReader.Close()
    End Sub

    Private Sub AddXMLData()
        Dim xd As New XmlDocument
        xd.Load("C:\Temp\B1S6AFSData.xml")
        Dim nod1 As XmlElement = xd.DocumentElement
        Dim nod2 As XmlElement = xd.DocumentElement
        Dim nod3 As XmlElement = xd.DocumentElement
        Dim nod4 As XmlElement = xd.DocumentElement
        Dim nod5 As XmlElement = xd.DocumentElement
        Dim nod6 As XmlElement = xd.DocumentElement
        Dim nod7 As XmlElement = xd.DocumentElement
        Dim nod8 As XmlElement = xd.DocumentElement
        Dim nod9 As XmlElement = xd.DocumentElement
        Dim nod10 As XmlElement = xd.DocumentElement
        Dim nod11 As XmlElement = xd.DocumentElement
        Dim nod12 As XmlElement = xd.DocumentElement
        Dim nod13 As XmlElement = xd.DocumentElement
        Dim nod14 As XmlElement = xd.DocumentElement
        Dim nod15 As XmlElement = xd.DocumentElement
        Dim nod16 As XmlElement = xd.DocumentElement
        Dim nod17 As XmlElement = xd.DocumentElement
        Dim nod18 As XmlElement = xd.DocumentElement
        Dim nod19 As XmlElement = xd.DocumentElement
        Dim nod20 As XmlElement = xd.DocumentElement
        Dim nod21 As XmlElement = xd.DocumentElement
        Dim nod22 As XmlElement = xd.DocumentElement
        Dim nod23 As XmlElement = xd.DocumentElement
        Dim nod24 As XmlElement = xd.DocumentElement
        Dim nod25 As XmlElement = xd.DocumentElement
        Dim nod26 As XmlElement = xd.DocumentElement

        Dim nod30, nod31, nod32, nod33, nod34, nod35, nod36, nod37, nod38, nod39, nod40, _
            nod41, nod42, nod43, nod44, nod45, nod46, nod47, nod48, nod49, nod50, nod51, _
            nod52, nod53, nod54, nod55 As XmlNode

        nod30 = nod1.SelectSingleNode("Answer1")
        nod30.InnerText = MainForm.strB1S6A(1)
        nod31 = nod2.SelectSingleNode("Answer2")
        nod31.InnerText = MainForm.strB1S6A(2)
        nod32 = nod3.SelectSingleNode("Answer3")
        nod32.InnerText = MainForm.strB1S6A(3)
        nod33 = nod4.SelectSingleNode("Answer4")
        nod33.InnerText = MainForm.strB1S6A(4)
        nod34 = nod5.SelectSingleNode("Answer5")
        nod34.InnerText = MainForm.strB1S6A(5)
        nod35 = nod6.SelectSingleNode("Answer6")
        nod35.InnerText = MainForm.strB1S6A(6)
        nod36 = nod7.SelectSingleNode("Answer7")
        nod36.InnerText = MainForm.strB1S6A(7)
        nod37 = nod8.SelectSingleNode("Answer8")
        nod37.InnerText = MainForm.strB1S6A(8)
        nod38 = nod9.SelectSingleNode("Answer9")
        nod38.InnerText = MainForm.strB1S6A(9)
        nod39 = nod10.SelectSingleNode("Answer10")
        nod39.InnerText = MainForm.strB1S6A(10)
        nod40 = nod11.SelectSingleNode("Answer11")
        nod40.InnerText = MainForm.strB1S6A(11)
        nod41 = nod12.SelectSingleNode("Answer12")
        nod41.InnerText = MainForm.strB1S6A(12)
        nod42 = nod13.SelectSingleNode("Answer13")
        nod42.InnerText = MainForm.strB1S6A(13)
        nod43 = nod14.SelectSingleNode("Answer14")
        nod43.InnerText = MainForm.strB1S6A(14)
        nod44 = nod15.SelectSingleNode("Answer15")
        nod44.InnerText = MainForm.strB1S6A(15)
        nod45 = nod16.SelectSingleNode("Answer16")
        nod45.InnerText = MainForm.strB1S6A(16)
        nod46 = nod17.SelectSingleNode("Answer17")
        nod46.InnerText = MainForm.strB1S6A(17)
        nod47 = nod18.SelectSingleNode("Answer18")
        nod47.InnerText = MainForm.strB1S6A(18)
        nod48 = nod19.SelectSingleNode("Answer19")
        nod48.InnerText = MainForm.strB1S6A(19)
        nod49 = nod20.SelectSingleNode("Answer20")
        nod49.InnerText = MainForm.strB1S6A(20)
        nod50 = nod21.SelectSingleNode("Answer21")
        nod50.InnerText = MainForm.strB1S6A(21)
        nod51 = nod22.SelectSingleNode("Answer22")
        nod51.InnerText = MainForm.strB1S6A(22)
        nod52 = nod23.SelectSingleNode("Answer23")
        nod52.InnerText = MainForm.strB1S6A(23)
        nod53 = nod24.SelectSingleNode("Answer24")
        nod53.InnerText = MainForm.strB1S6A(24)
        nod54 = nod25.SelectSingleNode("Answer25")
        nod54.InnerText = MainForm.strB1S6A(25)
        nod55 = nod26.SelectSingleNode("Answer26")
        nod55.InnerText = MainForm.strB1S6A(26)

        xd.Save("C:\Temp\B1S6AFSData.xml")
    End Sub
End Class